//
//  OutlineViewExtension.m
//

#import "OutlineViewExtension.h"


@implementation OutlineViewExtension

- (void)textDidEndEditing:(NSNotification *)notification;
{
	if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement)
	{
		// This is ugly, but just about the only way to do it. NSOutlineView is 
		// determined to select and edit something else, even the text field that it just
		// finished editing, unless we mislead it about what key was pressed to end editing. 
		NSMutableDictionary *newUserInfo;
		NSNotification *newNotification;

		newUserInfo = [NSMutableDictionary dictionaryWithDictionary:[notification userInfo]];
		[newUserInfo setObject:[NSNumber numberWithInt:NSIllegalTextMovement] forKey:@"NSTextMovement"];
		newNotification = [NSNotification notificationWithName
			:[notification name]
			object:[notification object]
			userInfo:newUserInfo];
		[super textDidEndEditing:newNotification];

		// For some reason we lose firstResponder status when when we do the above.
		[[self window] makeFirstResponder:self];
	}
	else
	{
		[super textDidEndEditing:notification];
	}
} 

- (void)keyDown:(NSEvent *)event
{
	if ([self numberOfRows] > 0)
	{
		int selRow = [self selectedRow];
		if (-1 != selRow)
		{
			unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
			unsigned int flags = [event modifierFlags] & 0xFFFFFE00;
			if (key == NSCarriageReturnCharacter && flags == 0)
			{
				[self editColumn:0  row:selRow  withEvent:nil  select:YES];
				return;
			}
		}
	}

	[super keyDown:event]; // let somebody else handle the event
}

@end
